<template>
  <div class="all">
    <div class="app">
      <div>
        <el-form ref="form" :model="form" label-width="80px" inline>
          <el-form-item>
            <el-select
              v-model="form.region"
              placeholder="请选择查询条件"
              class="query"
            >
              <el-option label="申请人" value="name"></el-option>
              <el-option label="申请账号" value="username"></el-option>
              <el-option label="联系方式" value="phone"></el-option>
              <el-option label="申请状态" value="status"></el-option>
            </el-select>
            <el-form-item>
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-button
              type="primary"
              class="referBtn el-icon-search"
              @click="searchHandle"
            >
              搜索</el-button
            >
            <el-button type="primary" class="referBtn" @click="resetHandle"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div>
      <el-table
        :border="true"
        ref="filterTable"
        :data="adminList"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="50" align="center">
        </el-table-column>
        <el-table-column prop="name" label="申请人" width="150" align="center">
        </el-table-column>

        <el-table-column
          prop="username"
          label="申请账号"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="position" label="账号类型" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="联系方式" align="center">
        </el-table-column>

        <el-table-column
          prop="status"
          label="申请状态"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.status === '申请中'
                  ? 'warning'
                  : 'success' && scope.row.status === '未通过'
                  ? 'danger'
                  : 'success'
              "
              disable-transitions
              >{{ scope.row.status }}</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              class="el-icon-edit"
              type="primary"
            >
              状态更改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="状态更改"
      :visible.sync="dialogVisible"
      width="20%"
      :before-close="handleClose"
    >
      <el-select v-model="boxValue" placeholder="请选择" class="box">
        <el-option
          v-for="(item, index) in options"
          :key="index"
          :label="item.text"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmHandle()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入 axios
// import Axios from "axios";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      dialogVisible: false, // 弹出框状态

      form: {
        region: "", // 查询选择项
        name: "", // 查询内容
      },

      options: [
        { text: "已通过", value: "已通过" },
        { text: "未通过", value: "未通过" },
      ],
      boxValue: "", //弹出框选择
      redactInfo: "", //编辑信息
    };
  },

  mounted() {
    let obj = { type: "position", value: "门店管理员" };
    this.search(obj); // 调用查询请求参函数
  },

  computed: {
    // 获取 adminList 数据
    ...mapGetters({
      adminList: "admin/getAdminListGetter",
      page: "admin/getAdminPageGetter",
    }),
  },

  methods: {
    // 查询请求参函数
    search(obj) {
      this.$store.dispatch("admin/getAdminConditionAction", obj);
    },
    // // 查询选项
    filterTag(value, row) {
      console.log(row);
      return row.state === value;
    },

    // 显示条数更换
    handleSizeChange(val) {
      let obj = { size: val };
      this.search(obj);
    },

    // 每页点击更换
    handleCurrentChange(val) {
      let obj = { current: val };
      this.search(obj);
    },

    // 查询按钮
    searchHandle() {
      let obj = {
        type: this.form.region,
        value: this.form.name,
        type2: "position",
        value2: "门店管理员",
      };
      this.search(obj);
      this.form.region = this.form.name = "";
    },
    // 重置按钮
    resetHandle() {
      let obj = {
        type: "position",
        value: "门店管理员",
      };
      this.search(obj);
    },

    // 编辑按钮
    handleEdit(index, row) {
      console.log("当前行信息", row);
      this.redactInfo = row; // 当前行信息
      this.boxValue = "";
      this.dialogVisible = true;
    },

    // 弹出框右上角关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },

    // 弹出框确认按钮
    confirmHandle() {
      if (this.boxValue) {
        this.redactInfo.status = this.boxValue;
      }

      let arr = [
        { id: this.redactInfo._id },
        { status: this.redactInfo.status },
      ];
      console.log("arr", arr);
      this.$store.dispatch("admin/updateAdminAction", arr);

      this.dialogVisible = false;
    },
  },
};
</script>

<style scoped>
.all {
  margin: 50px;
}
.app {
  display: flex;
}

.query {
  width: 150px;
  margin-right: 10px;
}
.box {
  margin-left: 10%;
  width: 80%;
}
.block {
  margin-top: 20px;
}
</style>
